// pages/goodsSearch/goodsSearch.js
import request from "../../utils/request";
import { getStorage } from "../../utils/utils"
const api = require("../../config/api");
import {
	shareOptions
} from "../../utils/access";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList: [],
    current: 1,
    pages: 0,
    latitude: "",
		longitude: "",
    isLoaing: false,
    goodsDesc:"",
    scrollTopFlag:false,
    communityId: getStorage('communityId')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
	//获取全部商品列表
	async getGoodsList() {
		const lat = getStorage("latitude");
    const lon = getStorage("longitude");
    const { province,  city, direct  } = getStorage('addressInfo') || {}
		let {
      current,
      goodsDesc
		} = this.data;
		const {
			data
		} = await request.get(api.allGoods, {
			page: current,
			size: 10,
			lat,
			lon,
      type: 4,
      goodsDesc,
      provinceName: province || '',
      cityName: city|| '',
      districtName: direct || ''
		});
		this.setData({
			goodsList: (this.data.current === 1 ?
				data.records || [] :
				[...this.data.goodsList, ...data.records]).map(item => ({
          ...item,
          goodsSigns: (item.goodsSignList || item.goodsSigns)?.map(sign => sign.replace('#', ''))
        })),
			total: data.total,
			current: data.current,
			pages: data.pages,
		});

		wx.stopPullDownRefresh();
	},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //搜索值
  handleSearchName(e){
    this.setData({
      goodsDesc: e.detail.value
    })
  },
  //点击搜索
  goodsSearch(){
    let {goodsDesc} = this.data
    if(!goodsDesc) return
    this.setData({
      current: 1
    })
    this.getGoodsList()
  },
  //点击回到顶部
  handleTop(){
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 300
    })
  },
  //跳转详情
  handleGoodsDetail(event) {
		wx.navigateTo({
			url: `/pages/idleGosDetail/idleGosDetail?goodsId=` + event.currentTarget.id,
		});
	},
  onPageScroll(e){
    const height = wx.getSystemInfoSync().windowHeight
    // 获取当前手机屏幕尺寸
    if(e.scrollTop > height) {
      console.log(`大于了${height}`)
        this.setData({
          scrollTopFlag:true
        })
    }else {
      this.setData({
        scrollTopFlag:false
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getGoodsList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom: function () {
    console.log(111)
		let {
			current,
			pages
		} = this.data;
		// page是当前页码乘以默认值 ==》当前数据总量
		if (current < pages) {
			// current 当前页码值
			this.setData({
				current: this.data.current + 1,
			});
			// 调用接口，发送请求，获得后续的数据
				this.getGoodsList(current);
		} else {
			this.setData({
				isLoaing: true,
			});
		}
	},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    return shareOptions('default', e)
  }
})